#!/bin/sh

if [ -n "$1" ]; then
    data_date=$1
else
  data_date=$(date -d "-1 days" +%F)
fi


LOAD_DATA_DWS="
insert overwrite table xm1_dws.dws_session_log partition (dt='${data_date}')
select
    deviceid,devicetype,sessionid,province_name,city_name,
    count(sessionid) as count_session,
    max(\`timestamp\`)-min(\`timestamp\`) as session_during_time,
    split(
    sort_array(
collect_list(
    if(eventid='pageView',concat(\`timestamp\`,'-',properties['url']),null)))
        [0],'-')[1]as session_entry_url
    from xm1_dwd.dwd_event where dt='${data_date}'
group by deviceid, devicetype, sessionid, province_name, city_name;"

/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${LOAD_DATA_DWS}"